// pages/myAlbum/myAlbum.js
const myAudio = wx.getBackgroundAudioManager()
import api from '../../utils/api.js'
import util from '../../utils/util.js'
//获取应用实例
const app = getApp()
var page = 1
var uid = 0
var shareData = {}
Page({
  data: {
    haveMore: true,
    noData: false,
    myAlbum: [],
    haveAd: false,
    unitid: '',
  },

  onLoad: function (options) {
    wx.showLoading({
      title: '加载中',
    })
    this.getAd()
    const uidData = wx.getStorageSync('uid')
    if (uidData) { 
      page = 1
      uid = uidData.uid
      this.getAlbumData(uid)
    }else{
      wx.hideLoading()
      this.setData({
        noData: true,
        haveMore: false,
      })
    }
  },
  onShow(){
    // 获取分享数据 
    shareData = util.getShareData()
  },
  // 获取我的相册数据
  getAlbumData(uid){
    let that = this
    const data = {
      uid,
      m:'MyPhoto',
      length:'10',
      page,
    }
    api.getData(data).then(res=>{
      if(res.data.code == 1){
        // 错误处理
        if (res.data.data.length == 0){
          that.setData({ noData: true })
          wx.hideLoading()
          return
        }
        const myAlbum = res.data.data.list
        if (myAlbum.length != 0){
          if (myAlbum.length < 10) { that.setData({ haveMore: false, noData: false })}
          for(let i = 0; i< myAlbum.length; i++){
            myAlbum[i].urls = JSON.parse(myAlbum[i].urls)
          }
          that.setData({ 
            myAlbum: [...this.data.myAlbum, ...myAlbum],
          })
        }else{
          that.setData({ noData: true })
        }
        wx.hideLoading()
      }else{
        wx.hideLoading()
        wx.showToast({
          title: '抱歉，数据请求错误',
          icon: 'none',
          duration: 2000
        })
      }
    })
  },
  // 删除模板
  delectTem(e){
    let that = this
    const id = e.currentTarget.dataset.id
    const index = e.currentTarget.dataset.index
    wx.showModal({
      title: '提示',
      content: '您确定要删除该相册吗？',
      success(res) {
        if (res.confirm) {
          const data = {
            id,
            m: 'DelPhoto'
          }
          api.getData(data).then(res => {
            if (res.data.code == 1) {
              let myAlbum = that.data.myAlbum
              myAlbum.splice(index, 1)
              that.setData({ myAlbum })
            }
          })
        }
      }
    })
  },
  // 获取广告
  getAd() {
    let that = this
    const data = wx.getStorageSync('advertise')
    if (data) {
      const advertise = data.advertise
      if (advertise[1]) {
        const adData = advertise[1]
        if (adData.is_show == 'true' && adData.unitid) {
          if (adData.unitid == '') { return }
          that.setData({
            haveAd: true,
            unitid: adData.unitid
          })
        }
      }
    }
  },
  // 转跳到首页进行查看
  turnToSee(e){
    const pid = e.currentTarget.dataset.pid
    wx.reLaunch({
      url: `/pages/index/index?pid=${pid}`,
    })
  },
  // 触底
  onReachBottom: function () {
    if (this.data.haveMore == false){
      return
    }
    page++
    this.getAlbumData(uid)
  },
  onShareAppMessage: function () {
    return {
      title: shareData.title,
      imageUrl: shareData.imageUrl,
      path: `pages/index/index`
    }
  }
})